﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows;

namespace Zen4SyncDashboard.ViewModel
{
    public class MainWindowViewModel : BaseViewModel
    {
        public BaseViewModel ActiveViewModel { get; private set; }

        #region - Commands -

        public RelayCommand ExitApplicationCommand { get; private set; }

        public RelayCommand NavigateMergeSyncCommand { get; private set; }

        public RelayCommand NavigateAllTestScenariosCommand { get; private set; }
        public RelayCommand NavigateCreateTestScenarioCommand { get; private set; }

        public RelayCommand NavigateMergeSyncContextCommand { get; private set; }
        
        public RelayCommand NavigateCreateTestSessionCommand { get; private set; }
        public RelayCommand NavigateAllTestSessionsCommand { get; private set; }
        
        public RelayCommand NavigateTestServerCommand { get; private set; }

        #endregion - Commands -
        
        public MainWindowViewModel()
        {
            InitializeCommands();
            InitializeBoundProperties();
        }

        void ActiveViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if ("WorkStatus" == e.PropertyName)
            {
                base.SetWorkStatus(ActiveViewModel.WorkStatus);
                base.OnPropertyChanged("WorkStatus");
            }
        }

        #region - Initializations Methods -
        
        private void InitializeCommands()
        {
            ExitApplicationCommand = new RelayCommand(
                param => ExitApplication());

            NavigateMergeSyncCommand = new RelayCommand(
                param => SetActiveViewModel(new MergeSyncViewModel()),
                param => !(ActiveViewModel is MergeSyncViewModel));

            NavigateAllTestScenariosCommand = new RelayCommand(
                param => SetActiveViewModel(new AllTestScenariosViewModel()),
                param => !(ActiveViewModel is AllTestScenariosViewModel));

            NavigateCreateTestScenarioCommand = new RelayCommand(
                param => SetActiveViewModel(new CreateTestScenarioViewModel()),
                param => !(ActiveViewModel is CreateTestScenarioViewModel));

            NavigateMergeSyncContextCommand = new RelayCommand(
                param => SetActiveViewModel(new MergeSyncContextViewModel()),
                param => !(ActiveViewModel is MergeSyncContextViewModel));

            NavigateCreateTestSessionCommand = new RelayCommand(
                param => SetActiveViewModel(new CreateTestSessionViewModel()),
                param => !(ActiveViewModel is CreateTestSessionViewModel));

            NavigateTestServerCommand = new RelayCommand(
                param => SetActiveViewModel(new TestServerViewModel()),
                param => !(ActiveViewModel is TestServerViewModel));

            NavigateAllTestSessionsCommand = new RelayCommand(
                param => SetActiveViewModel(new AllTestSessionsViewModel()),
                param => !(ActiveViewModel is AllTestSessionsViewModel));
        }       

        private void InitializeBoundProperties()
        {
            base.SetWorkStatus(false);
            base.OnPropertyChanged("WorkStatus");
        }

        #endregion - Initializations Methods -

        private void ExitApplication()
        {
            Application.Current.Shutdown();
        }

        private void SetActiveViewModel(BaseViewModel vm)
        {
            if (ActiveViewModel != null)
                ActiveViewModel = null;

            ActiveViewModel = vm;
            // Subscribe to PropertyChanged event to propagate the updates of the WorkStatus.
            vm.PropertyChanged += new PropertyChangedEventHandler(ActiveViewModel_PropertyChanged);
            OnPropertyChanged("ActiveViewModel");
        }
    }
}
